<template>
  <div>
    <!-- 分类页面 -->
    <Search :mess="mess" />
    <van-tree-select
      :items="types"
      height="100vh"
      :main-active-index.sync="activeIndex"
      :active-id="reightid"
      @click-nav="leftfn"
    >
      <template slot="content">
        <div ref="right">
          <div v-for="(item, index) in contentsrigt" :key="index" class="love">
            <img :src="item.image" alt="" />
            <h3 class="text">{{ item.text }}</h3>
          </div>
        </div>
      </template>
    </van-tree-select>
    <TabBar />
  </div>
</template>
<script>
import TabBar from "../components/TabBar.vue";
import Search from "../components/Search.vue";
import instance from "@/api/api";
export default {
  name: "type",
  data() {
    return {
      mess: "分类页",
      types: [], //存放数据
      activeIndex: 0, //左侧默认选中的字段
      reightid: "",
      contentsrigt: [], //右侧数据
    };
  },
  methods: {
    leftfn(item) {
      //点击左侧导航触发的事件
      console.log(item); //点击当前元素的索引
      this.activeIndex = item;
      this.contentsrigt = this.types[item].children;
      //获取右侧元素
      var xx = document.getElementsByClassName("van-tree-select__content");
      xx[0].scrollTop = 0;
      console.log(xx[0].scrollTop);
    },
    async gettypes() {
      //请求数据的函数
      let { data } = await instance.get("/type");
      console.log(data);
      this.types = data;
      this.contentsrigt = this.types[this.activeIndex].children;
    },
  },
  created() {
    this.gettypes();
  },
  components: {
    TabBar,
    Search,
  },
};
</script>

<style scoped lang="less">
.love {
  float: left;
  margin: 10px;
  .text {
    margin-top: 10px;
  }
}
.van-sidebar::-webkit-scrollbar {
  display: none;
}
.van-tree-select__content::-webkit-scrollbar {
  display: none;
}
</style>